<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" :label-position="labelPosition" label-width="100px">
      <el-form-item label="资格名称" prop="expertisePost">
        <el-input v-model="form.expertisePost" />
      </el-form-item>
      <el-form-item label="证书取得时间" prop="expertiseStart">
        <el-date-picker
          style="width: 100%"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          v-model="form.expertiseStart"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="证书有效期限" prop="expertiseEnd">
        <el-date-picker
          style="width: 100%"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          v-model="form.expertiseEnd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="等级" prop="expertiseLevel">
        <el-input v-model="form.expertiseLevel" />
      </el-form-item>
      <el-form-item label="附件" prop="photos">
        <el-upload
          accept=".jpg, .png, .jpeg, .gif"
          action="https://jsonplaceholder.typicode.com/posts/"
          :multiple="true"
          :on-preview="handlePreview"
          :file-list="fileList"
          :on-remove=" handleRemove"
          :http-request="customRequest"
        >
          <el-button>
            <i class="el-icon-upload" />上传
          </el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button @click="handleSure" type="primary">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
    <preview-modal ref="previewModal" />
  </el-dialog>
</template>

<script>
import { setSession, getSession } from '@/utils/util'
import { createExpertise, updateExpertise } from '@/api/archives'
import { openNotification } from '@/utils/util'
import PreviewModal from '@/components/PreviewModal'
import { saveOnlyUploads } from '@/api/supplier'

export default {
  components: {
    PreviewModal
  },
  data() {
    return {
      labelPosition: 'right',
      isEdit: false,
      visible: false,
      form: {},
      fileList: [],
      userRow: null
    }
  },
  created() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
  },
  methods: {
    edit(bool, obj) {
      this.isEdit = bool
      this.visible = true
      this.form = Object.assign({}, obj)
      if (bool) {
        this.fileList = obj.savePath
      }
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.$set(this.form, 'userId', this.userRow.userId)
          let res = null,
            mes = null
          if (this.isEdit) {
            res = await updateExpertise(this.form)
            mes = '修改操作成功'
          } else {
            res = await createExpertise(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    // 预览图片
    handlePreview(file) {
      console.log('file', file)
      let arr = []
      this.fileList.map(v => {
        arr.push(v.url)
      })
      this.$refs.previewModal.show(arr)
      // console.log('tableData', this.itemArr[index].tableData, ind, one)
    },
    // 删除图片事件处理
    handleRemove(file, fileList, row, item) {
      console.log(file, fileList)
      this.fileList = [...fileList]
    },
    // 上传图片事件处理
    customRequest(info) {
      // const _this = this
      console.log('customRequest', info)
      const formData = new FormData()
      formData.append('file', info.file)
      // 调方法，post，body 放入 formData
      // this.fileList = []

      saveOnlyUploads(formData).then(res => {
        if (res.data !== null) {
          const { data } = res
          this.fileList.push(data)
          this.fileList[this.fileList.length - 1].url = data.fileUrl + data.fileName
          this.fileList[this.fileList.length - 1].savePath = data.fileUrl + data.fileName
          this.fileList[this.fileList.length - 1].name = data.uploadTitle

          console.log('fileList', this.fileList)

          let arr = []
          this.fileList.map(v => {
            arr.push(v.uploadId)
          })

          this.$set(this.form, 'photos', arr.join(','))
        }
      })
    }
  }
}
</script>
